// pages/commander/commander.js
var app = getApp();
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var page = 1;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isTimeOut: false,
    inptUserInfo: {
      name: '',
      mobile: '',
      code: '',
    },
    userInfo: {},
    isShowWithDraw: false,
    requestCode: '',
    timeout: 60,
    codeText: '获取验证码',
    myPromotion: [],
    setting: {},
    orderList: [],
    withdrawMoney: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.requestUserInfo();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    page = 1;
    let arr = [];
    let title = ['可提现', '审核中', '已提现'];
    let keys = ['withdrawals', 'commander_apply', 'commander_withdraw'];
    wx.request({
      url: commonURL + '&op=getcommissioninfo',
      data: {
        uniacid: app.data.GZHid,
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
        openid: app.data.openid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          area_commander_settled: res.data.info.area_commander_settled,
          area_commander_apply: res.data.info.area_commander_apply,
          area_commander_withdraw: res.data.info.area_commander_withdraw
        })
      }
    })
    that.setData({
      myPromotion: arr
    })
    wx.request({
      url: commonURL + 'op=getcommission',
      data: {
        areaid: wx.getStorageSync('xiaoqu').id || wx.getStorageSync('tzInfo').area_id,
        openid: app.data.openid,
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
      },
      success: function (res) {
        console.log('111')
        console.log(res)
        that.setData({
          orderList: res.data.list
        })
      }
    })
  },
  onReachBottom: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    page++;
    wx.request({
      url: commonURL + 'op=getcommission',
      data: {
        uniacid: app.data.GZHid,
        areaid: wx.getStorageSync('xiaoqu').id || wx.getStorageSync('tzInfo').area_id,
        openid: app.data.openid,
        page: page,
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
        size: '10'
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res)
        that.setData({
          orderList: that.data.orderList.concat(res.data.list)
        })
      }
    })
  },
  requestApplyWithDrawals() {
    var that = this;                                       //申请提现
    if (Number(this.data.withdrawMoney) < 1 || Number(this.data.withdrawMoney) > this.data.area_commander_settled) {
      wx.showToast({
        title: '提现金额不正确',
        icon: 'loading',
        duration: 2000
      })
    }
    wx.request({
      url: commonURLXX + 'op=apply_area_commission',
      data: {
        uniacid: app.data.GZHid,
        openid: app.data.openid,
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
        price: this.data.withdrawMoney
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.status == 1) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            complete: function (res) {
              that.setData({
                isShowWithDraw: false
              })
              that.onShow();
              that.onLoad();
            }
          })
        }
      }
    })
  },
  inputWithdrawMoney(e) {
    this.setData({ withdrawMoney: e.detail.value })
  },
  toggleWithDraw() {
    if (this.data.isShowWithDraw) {

    } else {
      this.setData({
        isShowWithDraw: true,
      })
    }
  },
})